﻿
/**
* 右键菜单插件类；
*/



define(function (require, exports, module) {

    FXREADER.CContextMenuPlugin = function (pReaderApp) {
        var _pReaderApp = pReaderApp,
             _MenuContainerID = "FxMenuContainer",
             _MenuID = "FxMenu",
             _bMenuVisable = false,
             _Language = _pReaderApp.GetLanguage(),
             _self = this;

        //获取Plugin名称;
        this.GetPluginName = function () {
            return "ContextMenu Plugin";
        }
        //插件被注册的时候调用;
        this.PIOnRegister = function () {
        }
        //模块初始化;
        this.PIInit = function () {
            var menuHtml = "<div id='" + _MenuContainerID + "'><ul id='" + _MenuID + "' class='jeegoocontext cm_default' style='left: 341px; top: 167px; display: none;'></div>";
            $("#" + _pReaderApp.GetMainView().GetMainFrameID()).after(menuHtml);
            _self.CreateCmdAndMenuItem();
            var menuItemsHtml = "";
            for (var i = 0; i < FXREADER.Config.MenuBar.length; i++) {
                var pluginName = FXREADER.Config.MenuBar[i].plugin;
                var pluginFlag = FXREADER.Config.Plugins.load[pluginName];
                if (pluginFlag != null && pluginFlag == false) {
                    continue;
                }
                var menuItems = FXREADER.Config.MenuBar[i].items;
                for (var k = 0; k < menuItems.length; k++ )
                    menuItemsHtml += _self.CreateMenuItemHtml(menuItems[k]);
            }
            $("#" + _MenuID).append(menuItemsHtml);

            $("#" + _pReaderApp.GetMainView().GetDocView().GetDocViewContainerID()).bind('contextmenu', function (e) { if (_self.IsMenuVisible()) _self.HiddenMenu(); return false; });
            $("#" + _pReaderApp.GetMainView().GetDocView().GetDocViewContainerID()).jeegoocontext(_MenuID, { onSelect: function (e, context) { return _self.OnSelect($(this)); },
                onShow: function (e, context) { return _self.OnShow(); },
                onHide: function (e, context) { return _self.OnHidden(); },
                onHover: function (e, context) { return true; }
            });
            _pReaderApp.StepProgress();
        }

        this.CreateMenuItemHtml = function (jsonMenuItem) {
            var menuItemHtml = "";
            if (!jsonMenuItem)
                return jsonMenuItem;
            var pMenuItem = _pReaderApp.GetMenuItem(jsonMenuItem.item);
            if (pMenuItem == null)
                return "";
            menuItemHtml += pMenuItem.CreateHtml();
            if (menuItemHtml == "")
                return menuItemHtml;
            var jqMenuItem = $(menuItemHtml);
            var bHasSubItem = false;
            var subItemsHtml = "";
            if (jsonMenuItem.subitems && jsonMenuItem.subitems.length > 0) {
                for (var i = 0; i < jsonMenuItem.subitems.length; i++) {
                    var jsonChildMenuItem = jsonMenuItem.subitems[i];
                    var childMenuHtml = _self.CreateMenuItemHtml(jsonChildMenuItem);
                    if (childMenuHtml != "") {
                        bHasSubItem = true;
                        subItemsHtml += childMenuHtml;
                    }
                }
            }
            if (bHasSubItem) {
                jqMenuItem.append("<ul>" + subItemsHtml + "</ul>");
            }

            menuItemHtml = jqMenuItem.get(0).outerHTML;
            return menuItemHtml;
        }

        this.OnShowMenuItems = function (jsonMenuItem) {
            if (!jsonMenuItem)
                return jsonMenuItem;
            var pMenuItem = _pReaderApp.GetMenuItem(jsonMenuItem.item);
            if (pMenuItem == null)
                return;
            pMenuItem.OnShow();
            if (jsonMenuItem.subitems && jsonMenuItem.subitems.length > 0) {
                for (var i = 0; i < jsonMenuItem.subitems.length; i++) {
                    var jsonChildMenuItem = jsonMenuItem.subitems[i];
                    _self.OnShowMenuItems(jsonChildMenuItem);
                }
            }
        }

        //模块卸载;
        this.PIUnload = function () {
        }

        this.GetReaderApp = function () {
            return _pReaderApp;
        }

        this.CreateCmdAndMenuItem = function () {
            _pReaderApp.AddMenuItem({
                Name: "MenuSeparator",
                CreateHtmlContentFunc: function () {
                    return "<li class='separator'></li>";
                }
            });

            _pReaderApp.AddMenuItem({
                Name: "MenuZoom50",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmz50' class='icon' menuname='MenuZoom50'><span class='ok icon'></span>50%</li>";
                },
                OnShow: function () {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    var jqZoom = $("#cmz50");
                    jqZoom.find('.ok').remove();
                    if (scale == 0.5 && !_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().SetFitWidth(false);
                            _pReaderApp.GetMainView().GetDocView().OnZoom(0.5);
                        }
                        else
                            return false;
                    }
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuZoom75",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmz75' class='icon' menuname='MenuZoom75'><span class='ok icon'></span>75%</li>";
                },
                OnShow: function () {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    var jqZoom = $("#cmz75");
                    jqZoom.find('.ok').remove();
                    if (scale == 0.75 && !_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().SetFitWidth(false);
                            _pReaderApp.GetMainView().GetDocView().OnZoom(0.75);
                        }
                        else
                            return false;
                    }
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuZoom100",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmz100' class='icon' menuname='MenuZoom100'><span class='ok icon'></span>100%</li>";
                },
                OnShow: function () {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    var jqZoom = $("#cmz100");
                    jqZoom.find('.ok').remove();
                    if (scale == 1.0 && !_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().SetFitWidth(false);
                            _pReaderApp.GetMainView().GetDocView().OnZoom(1.0);
                        }
                        else
                            return false;
                    }
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuZoom125",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmz125' class='icon' menuname='MenuZoom125'><span class='ok icon'></span>125%</li>";
                },
                OnShow: function () {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    var jqZoom = $("#cmz125");
                    jqZoom.find('.ok').remove();
                    if (scale == 1.25 && !_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().SetFitWidth(false);
                            _pReaderApp.GetMainView().GetDocView().OnZoom(1.25);
                        }
                        else
                            return false;
                    }
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuZoom150",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmz150' class='icon' menuname='MenuZoom150'><span class='ok icon'></span>150%</li>";
                },
                OnShow: function () {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    var jqZoom = $("#cmz150");
                    jqZoom.find('.ok').remove();
                    if (scale == 1.5 && !_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().SetFitWidth(false);
                            _pReaderApp.GetMainView().GetDocView().OnZoom(1.5);
                        }
                        else
                            return false;
                    }
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuZoom200",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmz200' class='icon' menuname='MenuZoom200'><span class='ok icon'></span>200%</li>";
                },
                OnShow: function () {
                    var jqZoom = $("#cmz200");
                    jqZoom.find('.ok').remove();
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    if (scale == 2.0 && !_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().SetFitWidth(false);
                            _pReaderApp.GetMainView().GetDocView().OnZoom(2.0);
                        }
                        else
                            return false;
                    }
                }
            });

            _pReaderApp.AddMenuItem({
                Name: "FitWidth",
                CreateHtmlContentFunc: function () {
                    return "<li id='cmzFitwidth' class='icon' menuname='FitWidth'><span class='ok icon'></span>" + _Language.CommandUI.FitWidth + "</li>";
                },
                OnShow: function () {
                    var jqZoom = $("#cmzFitwidth");
                    jqZoom.find('.ok').remove();
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    if (_pReaderApp.GetMainView().GetDocView().IsFitWidth())
                        jqZoom.addClass('icon').prepend('<span class="icon ok"></span>');
                    else
                        jqZoom.removeClass('icon').find('.ok').remove();
                },
                OnSelect: function (jqMenuItem) {
                    var scale = _pReaderApp.GetMainView().GetDocView().GetScale();
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled")) {
                            _pReaderApp.GetMainView().GetDocView().FitWidth();
                        }
                        else
                            return false;
                    }
                }
            });

            _pReaderApp.AddMenuItem({
                Name: "MenuPrevPage",
                CreateHtmlContentFunc: function () {
                    return "<li  id='cm_prev_page' class='icon' menuname='MenuPrevPage'><span class='icon icon1'></span>" + _Language.Menu.PrevPage + "</li>";
                },
                OnShow: function () {
                    var jqMenuItem = $("#cm_prev_page");
                    if (_pReaderApp.GetMainView().GetDocView().GetCurPageIndex() == 0) {
                        jqMenuItem.addClass("disabled");
                    }
                    else {
                        jqMenuItem.removeClass("disabled");
                    }
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled"))
                            _pReaderApp.GetMainView().GetDocView().GoPrevPage();
                        else
                            return false;
                    }
                }
            });
            _pReaderApp.AddMenuItem({
                Name: "MenuNextPage",
                CreateHtmlContentFunc: function () {
                    return "<li  id='cm_next_page' class='icon' menuname='MenuNextPage'><span class='icon icon1'></span>" + _Language.Menu.NextPage + "</li>";
                },
                OnShow: function () {
                    var jqMenuItem = $("#cm_next_page");
                    if (_pReaderApp.GetMainView().GetDocView().GetCurPageIndex() == _pReaderApp.GetMainView().GetDocView().GetPageCount() - 1) {
                        jqMenuItem.addClass("disabled");
                    }
                    else {
                        jqMenuItem.removeClass("disabled");
                    }
                },
                OnSelect: function (jqMenuItem) {
                    if (jqMenuItem) {
                        if (!jqMenuItem.hasClass("disabled"))
                            _pReaderApp.GetMainView().GetDocView().GoNextPage();
                        else
                            return false;
                    }
                }
            });

        }


        this.IsMenuVisible = function () { return _bMenuVisable; }

        this.HiddenMenu = function () {
            $("#" + _MenuID).trigger('click.jeegoocontext');
        }

        this.OnHidden = function () {
            _bMenuVisable = false;
        }

        this.OnShow = function () {
            _bMenuVisable = true;

            for (var i = 0; i < FXREADER.Config.MenuBar.length; i++) {
                var pluginName = FXREADER.Config.MenuBar[i].plugin;
                var pluginFlag = FXREADER.Config.Plugins.load[pluginName];
                if (pluginFlag != null && pluginFlag == false) {
                    continue;
                }
                var menuItems = FXREADER.Config.MenuBar[i].items;
                for (var k = 0; k < menuItems.length; k++)
                    _self.OnShowMenuItems(menuItems[k]);
            }

        }

        this.OnSelect = function (jqMenuItem) {
            if (jqMenuItem != null) {
                try {
                    var menuname = jqMenuItem.attr("menuname");
                    var pMenuItem = _pReaderApp.GetMenuItem(menuname, null);
                    if (pMenuItem) {
                        pMenuItem.OnSelect(jqMenuItem);
                    }
                }
                catch (ex) {
                }
            }
        }

    }





    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pContextMenuPlugin = new FXREADER.CContextMenuPlugin(pReaderApp);
        return pContextMenuPlugin;
    }

});
